﻿namespace Amber.Arch.Catty
{
    public class ServiceRegistry
    {
        /// <summary>
        /// 服务类型
        /// </summary>
        public Type ServiceType { get; }

        /// <summary>
        /// 生命周期
        /// </summary>
        public LifeTime LifeTime { get; }

        /// <summary>
        /// 创建服务实例的工厂
        /// </summary>
        public Func<Cat, Type[], object?>? Factory { get; }

        internal ServiceRegistry? Next { get; set; }

        public ServiceRegistry(Type serviceType, LifeTime lifeTime, Func<Cat, Type[], object?>? factory)
        {
            ServiceType = serviceType;
            LifeTime = lifeTime;
            Factory = factory;
        }

        internal IEnumerable<ServiceRegistry> AsEnumerable()
        {
            var list = new List<ServiceRegistry>();
            for (var self = this; self != null; self = self.Next)
            {
                list.Add(self);
            }
            return list;
        }
    }
}
